<template>
  <div>
    <div class="handle-box">
      <el-button type="danger" round size="mini" @click="deleteNodeClick()"
        >批量删除</el-button
      >
    </div>
    <el-tree
      :data="muens"
      :props="defaultProps"
      :expand-on-click-node="false"
      show-checkbox
      node-key="catId"
      :default-expanded-keys="elKeys"
      ref="tree"
    >
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button
            v-if="node.level <= 2"
            type="text"
            @click="() => append(data)"
            >添加</el-button
          >
          <el-button
            v-if="node.childNodes.length == 0"
            type="text"
            @click="() => remove(node, data)"
            >删除</el-button
          >
          <el-button type="text" @click="() => edit(data)">编辑</el-button>
        </span>
      </span>
    </el-tree>

    <el-dialog
      :title="title"
      center
      :visible.sync="dialogVisible"
      width="30%"
      :close-on-click-modal="false"
    >
      <el-form :model="serchMap" label-width="15%">
        <el-form-item label="分类名称">
          <el-input v-model="serchMap.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      muens: [],
      elKeys: [],
      title: "",
      submitType: "",
      defaultProps: {
        children: "children",
        label: "name",
      },
      dataListLoading: false,
      dialogVisible: false,
      serchMap: {
        showStatus: 1,
        productCount: 0,
        sort: 0,
        name: "",
      },
    };
  },

  created() {
    this.getMenus();
  },

  methods: {
    //获取分类菜单
    getMenus() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/product/category/list/tree"),
        method: "get",
      }).then(({ data }) => {
        console.log("获取菜单数据==>>>", data);
        this.muens = data.data;
      });
    },

    //批量删除
    deleteNodeClick() {
      let arrycatId = [];
      let arryName = [];
      let o = this.$refs.tree.getCheckedNodes();

      if (o.length <= 0) {
        this.$message({
          message: "警告，不能删除空数据哦！请选择分类",
          type: "warning",
        });
      } else {
        console.log("分类维护节点选中信息==>>>", o);
        for (let i = 0; i < o.length; i++) {
          arrycatId.push(o[i].catId);
          arryName.push(o[i].name);
        }
        this.$confirm(`此操作将永久删除【${arryName}】是否继续?`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.$http({
              url: this.$http.adornUrl("/product/category/delete"),
              method: "post",
              data: this.$http.adornData(arrycatId, false),
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.getMenus();
                  },
                });
              } else {
                this.$message.error(data.msg);
              }
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      }
    },

    //表单提交判断
    submit() {
      if (this.submitType === "add") {
        //添加表单
        this.addSerchMap();
      }
      if (this.submitType === "edit") {
        //修改表单
        this.UpdateSerchmap();
      }
    },

    //修改分类
    UpdateSerchmap() {
      var { name, catId, icon, productUnit } = this.serchMap;
      this.$http({
        url: this.$http.adornUrl("/product/category/update"),
        method: "post",
        data: this.$http.adornData({ name, catId, icon, productUnit }, false),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message({
            message: "操作成功",
            type: "success",
            duration: 1500,
            onClose: () => {
              this.dialogVisible = false;
              this.getMenus();
              this.elKeys = [this.serchMap.parentCid];
            },
          });
        } else {
          this.$message.error(data.msg);
        }
      });
    },

    //修改弹窗
    edit(editdata) {
      console.log("要修改的数据==>>>", editdata);
      this.dialogVisible = true;
      this.submitType = "edit";
      this.title = "编辑";

      this.$http({
        url: this.$http.adornUrl(`/product/category/info/${editdata.catId}`),
        method: "get",
      }).then(({ data }) => {
        console.log("修改回显data数据==>>>", data.data.name);
        //回显数据
        this.serchMap.name = data.data.name;
        this.serchMap.catId = data.data.catId;
        this.serchMap.productUnit = data.data.productCount;
        this.serchMap.icon = data.data.icon;
        this.serchMap.parentCid = data.data.parentCid;
      });
    },

    //添加三级分类
    addSerchMap() {
      console.log("append", this.serchMap);
      this.$http({
        url: this.$http.adornUrl("/product/category/save"),
        method: "post",
        data: this.$http.adornData(this.serchMap, false),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message({
            message: "操作成功",
            type: "success",
            duration: 1500,
            onClose: () => {
              this.dialogVisible = false;
              this.getMenus();
              this.elKeys = [this.serchMap.parentCid];
            },
          });
        } else {
          this.$message.error(data.msg);
        }
      });
    },

    //添加弹框
    append(data) {
      console.log("添加数据:", data);
      this.dialogVisible = true;
      this.submitType = "add";
      this.title = "添加";
      this.serchMap.parentCid = data.catId;
      this.serchMap.catLevel = data.catLevel * 1 + 1;
      //数据清空
      this.serchMap.name = "";
      this.serchMap.catId = null;
      this.serchMap.icon = "";
      this.serchMap.productCount = 0;
    },

    //删除分类
    remove(node, data) {
      console.log(node, data);
      var ids = [data.catId];
      this.$confirm(`此操作将永久删除【${data.name}】是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/product/category/delete"),
            method: "post",
            data: this.$http.adornData(ids, false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getMenus();
                  this.elKeys = [node.parent.data.catId];
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style>
.handle-box {
  margin-bottom: 10px;
}
</style>